package com.yhj.reggie.service.impl;

import com.yhj.reggie.entity.User;
import com.yhj.reggie.exception.BusinessException;
import com.yhj.reggie.mapper.UserMapper;
import com.yhj.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;
    /**
     * 用户登录方法
     *
     * @param phone
     * @param userCode
     * @param sessionCode
     * @return
     */
    @Override
    public User loginUser(String phone, String userCode, String sessionCode) {
        //判断验证码是否正确
        if(!userCode.equals(sessionCode)){
            throw new BusinessException("验证码错误");
        }

        //根据号码找用户
        User loginUser = userMapper.findByPhone(phone);
        if (loginUser!=null){
            if (loginUser.getStatus() == 1){
                throw new BusinessException("用户已经被禁用");
            }
        }else {
            loginUser = new User();
            loginUser.setStatus(0);
            loginUser.setPhone(phone);
            userMapper.saveUser(loginUser);

        }


        return loginUser;
    }
}
